#admin/admin_class.py
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, IntegerField, SelectField, SelectMultipleField, TextAreaField, SubmitField
from wtforms.validators import DataRequired, Length, Optional, URL, Email
from wtforms.fields import DateTimeField



class LoginForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired(), Length(max=50)])
    password = PasswordField('密码', validators=[DataRequired()])
    remember_me = BooleanField('记住我')
    submit = SubmitField('登录')  # 添加提交按钮


class ChangePasswordForm(FlaskForm):
    current_password = PasswordField('当前密码', validators=[DataRequired()])
    new_password = PasswordField('新密码', validators=[
        DataRequired(),
        Length(min=8, message='密码长度至少8个字符')
    ])
    confirm_password = PasswordField('确认新密码', validators=[DataRequired()])
    submit = SubmitField('修改密码')

    def validate(self, extra_validators=None):
        # 首先执行基本验证
        if not super().validate(extra_validators=extra_validators):
            return False

        # 自定义验证逻辑
        if self.db_type.data == 'mysql':
            if not self.host.data:
                self.host.errors.append('MySQL 必须填写主机地址')
                return False
            if not self.port.data:
                self.port.errors.append('MySQL 必须填写端口')
                return False
            if not self.username.data:
                self.username.errors.append('MySQL 必须填写用户名')
                return False

        return True

        # 检查新密码和确认密码是否一致
        if self.new_password.data != self.confirm_password.data:
            self.confirm_password.errors.append('两次输入的密码不一致')
            return False

        return True


class DatabaseConfigForm(FlaskForm):
    db_type = SelectField('数据库类型', choices=[('sqlite', 'SQLite'), ('mysql', 'MySQL')], validators=[DataRequired()])
    host = StringField('主机地址', validators=[Optional()])
    port = IntegerField('端口', validators=[Optional()])
    username = StringField('用户名', validators=[Optional()])
    password = PasswordField('密码', validators=[Optional()])
    database = StringField('数据库名', validators=[DataRequired()])
    is_active = BooleanField('是否激活')

    def validate(self, extra_validators=None):
        # 首先执行基本验证
        if not super().validate(extra_validators=extra_validators):
            return False

class SystemConfigForm(FlaskForm):
    site_name = StringField('网站名称', validators=[DataRequired()])
    site_title = StringField('网站标题', validators=[DataRequired()])
    timezone = SelectField('时区设置', choices=[
        ('Asia/Shanghai', '中国标准时间 (UTC+8)'),
        ('UTC', '协调世界时 (UTC)'),
        ('America/New_York', '美国东部时间 (UTC-5)'),
        ('Europe/London', '伦敦时间 (UTC+0)')
    ], default='Asia/Shanghai')
    site_keywords = StringField('网站关键词')
    site_description = TextAreaField('网站描述')
    company_name = StringField('公司名称')
    company_address = StringField('公司地址')
    contact_phone = StringField('联系电话')
    contact_email = StringField('联系邮箱', validators=[Email()])
    footer_info = StringField('页脚信息')
    icp_number = StringField('ICP备案号')

    # 添加 operation_log_enabled 字段
    operation_log_enabled = BooleanField('启用操作日志')


    submit = SubmitField('保存')


class UserForm(FlaskForm):
    username = StringField('登录账号', validators=[DataRequired()])
    password_hash = PasswordField('密码')
    real_name = StringField('真实姓名')
    is_super = BooleanField('超级管理员')
    avatar = StringField('头像URL')
    member_id = SelectField('关联员工', coerce=int, choices=[(0, '无')])

    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        # 动态加载员工选项
        from inc.models import AdminMember
        members = AdminMember.query.all()
        self.member_id.choices = [(0, '无')] + [(m.id, m.name) for m in members]




class DepartmentForm(FlaskForm):
    name = StringField('部门名称', validators=[DataRequired(), Length(max=50)])
    parent_id = SelectField('父部门', coerce=int, validators=[Optional()])
    order_num = IntegerField('排序号', default=0)
    wecom_id = IntegerField('企业微信部门ID', validators=[Optional()])


class MemberForm(FlaskForm):
    name = StringField('姓名', validators=[DataRequired(), Length(max=50)])
    mobile = StringField('手机号', validators=[Optional(), Length(max=20)])
    email = StringField('邮箱', validators=[Optional(), Email()])
    department = SelectField('所属部门', coerce=int, validators=[Optional()])
    position = StringField('职位', validators=[Optional(), Length(max=50)])
    status = BooleanField('状态', default=True)
    wecom_userid = StringField('企业微信用户ID', validators=[Optional(), Length(max=64)], default=None)


class WechatConfigForm(FlaskForm):
    corp_id = StringField('企业微信Corp ID', validators=[DataRequired()])
    corp_secret = StringField('企业微信Corp Secret', validators=[DataRequired()])
    agent_id = StringField('企业微信Agent ID', validators=[Optional()])
    login_enabled = BooleanField('启用扫码登录', default=False)
    login_redirect_uri = StringField('登录回调URL', validators=[Optional(), URL()])
    submit = SubmitField('保存配置')
